<?php

/**
 * 
 * @desc Abstract validation rule 
 * @author Barbushin Sergey http://www.linkedin.com/in/barbushin
 * 
 */
abstract class Validator_RuleAbstract {
	
	protected $value;
	protected $errorText = 'not valid';

	public function __construct($errorText = null) {
		$this->setErrorText($errorText);
	}

	public function isValid($value) {
		$this->value = $value;
		return $this->isEmpty() || $this->validate();
	}

	protected function isEmpty() {
		return $this->value == '';
	}

	abstract protected function validate();

	public function getErrorText() {
		return str_replace('%value%', $this->value, $this->errorText);
	}

	public function setErrorText($errorText) {
		if ($errorText) {
			$this->errorText = $errorText;
		}
	}
}
